package com.yt.base.controller.order;

import com.alibaba.dubbo.common.utils.StringUtils;
import com.yt.base.controller.core.BaseController;
import com.yt.base.controller.order.vo.OInstitutionVo;
import com.yt.base.util.PinyinUtil;
import com.yt.core.MsgBean;
import com.yt.core.dao.filter.DynamicSQLFilterImpl;
import com.yt.core.dao.filter.QueryFilter;
import com.yt.order.api.interfaces.InstitutionService;
import com.yt.order.api.model.Institution;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/order/institution")
public class InstitutionController extends BaseController {

	@Autowired
	private InstitutionService institutionService;

	@RequestMapping("/list")
	public ModelAndView list(HttpServletRequest request, @RequestParam(value = "parentId", required = false) Long parentId) {
		ModelAndView mav = new ModelAndView("order/institution/InstitutionList");
		QueryFilter filter = new DynamicSQLFilterImpl();
		filter.addFilter(request.getParameterMap());
		String name = request.getParameter("name");
		if (StringUtils.isNotEmpty(name)) {
			filter.addFilter("Q_name_S_LK", name);
			mav.addObject("name", name);
		}
		if (parentId != null) {
			filter.addFilter("Q_parentId_L_EQ", parentId + "");
		}
		filter.setSortField("id", true);
		List<Institution> list = institutionService.find(filter);
		int count = institutionService.count(filter);

		List<OInstitutionVo> voList = new ArrayList<OInstitutionVo>();
		OInstitutionVo vo = null;
		for (Institution obj : list) {
			vo = new OInstitutionVo(obj);
			if (obj.getParentId() != null && obj.getParentId() != 0) {
				vo.setParent(institutionService.get(obj.getParentId()));
			}
			voList.add(vo);
		}
		processParent(mav, parentId);
		mav.addObject("list", voList);
		mav.addObject("total", count);
		return mav;
	}

	@RequestMapping("/toAdd")
	public ModelAndView toAdd(HttpServletRequest request, @RequestParam(value = "parentId", required = false) Long parentId) {
		ModelAndView mav = new ModelAndView("order/institution/InstitutionAdd");
		processParent(mav, parentId);
		return mav;
	}

	@RequestMapping("/toEdit")
	public ModelAndView toEdit(HttpServletRequest request, @RequestParam("id") Long id, @RequestParam(value = "parentId", required = false) Long parentId) {
		ModelAndView mav = new ModelAndView("order/institution/InstitutionAdd");
		Institution institution = institutionService.get(id);
		processParent(mav, institution.getParentId());
		mav.addObject("bean", institution);
		return mav;
	}


	@RequestMapping("/multiDel")
	public
	@ResponseBody
	String multiDel(@RequestParam(value = "ids", required = true) String ids) {
		String[] idArr = ids.split(",");
		if (idArr != null) {
			for (String id : idArr) {
				institutionService.remove(new Long(id));
			}
		}
		return "{success:true}";
	}

	@RequestMapping("/save")
	public
	@ResponseBody
	Object save(Institution institution, HttpServletRequest request) {
		MsgBean msg = new MsgBean();
		boolean pass = false;
		StringBuffer buff = new StringBuffer("{");
		if (institutionService.checkInstitutionName(institution.getName(), institution.getId())) {
			pass = true;
		} else {
			buff.append("机构名称已存在,请重新填写.");
		}
		if (institution.getDesc() != null) {
			if (institution.getDesc().length() >= 50) {
				institution.setDesc(institution.getDesc().substring(0, 50));
			}
		} else {
			institution.setDesc("");
		}
		if (pass) {
			institution.setPinyin(PinyinUtil.getPinyinJianPinToUpperCase(institution.getName()));
			if (institution.getId() != null) {
				institutionService.update(institution);
			} else {
				institutionService.insert(institution);
			}
		} else {
			msg.failure(buff.toString());
		}
		return msg.returnMsg();
	}

	@RequestMapping("/get")
	public
	@ResponseBody
	Object get(@RequestParam(value = "id", required = true) Long id) {
		MsgBean msg = new MsgBean();
		Institution institution = institutionService.get(id);
		msg.put("data", institution);

		msg.put("success", "true");
		return msg.returnMsg();
	}

	@RequestMapping("/showOrHide")
	public
	@ResponseBody
	String showOrHide(@RequestParam(value = "ids", required = true) String ids,
					  @RequestParam(value = "showOrHide", required = true) String showOrHide) {
		String[] idArr = ids.split(",");
		Institution inst;
		for (String id : idArr) {
			inst = institutionService.get(new Long(id));
			inst.setOnsale(showOrHide);
			institutionService.update(inst);
		}
		return "{success:true}";
	}

	@RequestMapping("/getJSON")
	public
	@ResponseBody
	Object getJSON(@RequestParam(value = "keyword", required = true) String keyword, HttpServletRequest request) {
		QueryFilter filter = new DynamicSQLFilterImpl();
		filter.addFilter("Q_parentId_L_EQ", "0");
		if (StringUtils.isNotEmpty(keyword)) {
			filter.addFilter("Q_pinyin_S_LK", PinyinUtil.getPinyinJianPinToUpperCase(keyword.trim()));
		}
		filter.setSortField("pinyin", false);
		filter.setPagingBean(null);
		List<Institution> list = institutionService.find(filter);
		MsgBean msgBean = new MsgBean();
		msgBean.put("data", list);
		return msgBean.returnMsg();
	}

	private void processParent(ModelAndView mav, Long parentId) {
		if (parentId != null) {
			mav.addObject("parentInstitution", institutionService.get(parentId));
			mav.addObject("parentId", parentId);
		}
	}
}
